package in.iqing.control.api.callback;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkError;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;

public abstract class BaseCallback implements ICallback {
    public static final int ERROR_CODE_UNKNOWN = -1;
    public static final int ERROR_CODE_SERVER = 600;
    public static final int ERROR_CODE_CONNECTION = 601;
    public static final int ERROR_CODE_NETWORK = 602;
    public static final int ERROR_CODE_PARSE = 603;
    public static final int ERROR_CODE_TIMEOUT = 604;
    public static final int ERROR_CODE_AUTH = 403;


    @Override
    public void onErrorResponse(VolleyError error) {
        if (error instanceof ServerError) {
            onFailure(ERROR_CODE_SERVER, error.getMessage());
        } else if (error instanceof NoConnectionError) {
            onFailure(ERROR_CODE_CONNECTION, error.getMessage());
        } else if (error instanceof NetworkError) {
            onFailure(ERROR_CODE_NETWORK, error.getMessage());
        } else if (error instanceof ParseError) {
            onFailure(ERROR_CODE_PARSE, error.getMessage());
        } else if (error instanceof TimeoutError) {
            onFailure(ERROR_CODE_TIMEOUT, error.getMessage());
        } else if (error instanceof AuthFailureError) {
            onFailure(ERROR_CODE_AUTH, error.getMessage());
        } else {
            onFailure(ERROR_CODE_UNKNOWN, error.getMessage());
        }
        onFinish();
    }

    @Override
    public void onResponse(String response) {
        try {
            onSuccess(response);
        } catch (Exception e) {
            onFailure(ERROR_CODE_UNKNOWN, e.getMessage());
        }
        onFinish();
    }


    @Override
    public void onStart() {

    }

    @Override
    public void onFinish() {

    }

}
